package com.project.decoration.controller.appointment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationAchievements;
import com.project.decoration.domain.Result;
import com.project.decoration.domain.User;
import com.project.decoration.service.DecorationAchievementsService;
import com.project.decoration.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 绩效controller
 * @date 2025/02/17 07:25
 */
@Controller
@ResponseBody
@RequestMapping("achievements")
public class DecorationAchievementsController {

    @Autowired
    private DecorationAchievementsService decorationAchievementsService;
    @Autowired
    private UserService userService;

    /** 分页获取绩效 */
    @PostMapping("getDecorationAchievementsPage")
    public Result getDecorationAchievementsPage(@RequestBody DecorationAchievements decorationAchievements) {
        Page<DecorationAchievements> page = new Page<>(decorationAchievements.getPageNumber(),decorationAchievements.getPageSize());
        QueryWrapper<DecorationAchievements> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationAchievements.getProjectId()),DecorationAchievements::getProjectId,decorationAchievements.getProjectId())
                .like(StringUtils.isNotBlank(decorationAchievements.getLoginAccount()),DecorationAchievements::getLoginAccount,decorationAchievements.getLoginAccount())
                .eq(StringUtils.isNotBlank(decorationAchievements.getUserId()),DecorationAchievements::getUserId,decorationAchievements.getUserId())
                .eq(decorationAchievements.getPrice() != null,DecorationAchievements::getPrice,decorationAchievements.getPrice());
        Page<DecorationAchievements> decorationAchievementsPage = decorationAchievementsService.page(page, queryWrapper);
        return Result.success(decorationAchievementsPage);
    }

    /** 根据id获取绩效 */
    @GetMapping("getDecorationAchievementsById")
    public Result getDecorationAchievementsById(@RequestParam("id")String id) {
        DecorationAchievements decorationAchievements = decorationAchievementsService.getById(id);
        return Result.success(decorationAchievements);
    }

    /** 保存绩效 */
    @PostMapping("saveDecorationAchievements")
    public Result saveDecorationAchievements(@RequestBody DecorationAchievements decorationAchievements) {
        String userId = decorationAchievements.getUserId();
        User user = userService.getById(userId);
        decorationAchievements.setLoginAccount(user.getLoginAccount());
        boolean save = decorationAchievementsService.save(decorationAchievements);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑绩效 */
    @PostMapping("editDecorationAchievements")
    public Result editDecorationAchievements(@RequestBody DecorationAchievements decorationAchievements) {
        String userId = decorationAchievements.getUserId();
        User user = userService.getById(userId);
        decorationAchievements.setLoginAccount(user.getLoginAccount());
        boolean save = decorationAchievementsService.updateById(decorationAchievements);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除绩效 */
    @GetMapping("removeDecorationAchievements")
    public Result removeDecorationAchievements(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationAchievementsService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("绩效id不能为空！");
        }
    }

}